<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends Admin {

	public function __construct()
    {
        parent::__construct();
		$this->load->model("category_model");


    }
    public function index(){
        $page = $this->uri->segment(4);
        $page_size = $this->getPageSize;
        $param = array();
        $param['keyword'] = $this->input->get('keyword');
        $param['parent'] = $this->input->get('parent');
        $param['child'] = $this->input->get('child');
        $total = $this->category_model->count_all($param);
        $records = $this->category_model->find_all($param,$page, $page_size);
        $page_url = base_url('/cp/category/index');
        $pagination = $this->get_search_paging($page_url, $total, $page, $page_size );
        $level = $this->category_model->getListLevel();
        $data = array(
            "total" => $total,
            "records" => $records,
            "pagination" => $pagination,
            'level'=>   $level,
            'param'=>$param
        );
        $this->layout->view($data);
    }



	public function add(){
		$data = array();
		$this->form_validation->set_rules('name', 'Name', 'required');
		if ($this->form_validation->run() == TRUE){
			$css = str_replace(' ','',removesign($this->input->post('name')));
			$data['name'] 			= trim($this->input->post('name'));
            if($this->input->post('parent')==0 && $this->input->post('child')==0){
               $data['level']=0;
               $data['parent']=0;
            }else if($this->input->post('parent')!=0 && $this->input->post('child')==0){
                $data['level']=$this->input->post('parent');
                $data['parent']=$this->input->post('parent');
            }else {
                $data['level']=$this->input->post('parent');
                $data['parent']=$this->input->post('child');
            }
			$data['orderNo'] 		= $this->input->post('orderNo');
			$data['status'] 		= 1;
			$categoryId = $this->category_model->insert($data);
			if($categoryId) {
				redirect('cp/category');
			}
		}
		$data['level'] = $this->category_model->getListLevel();

		$this->layout->view($data);
	}

	public function edit($id=null){
		$data = array();
		$this->form_validation->set_rules('name', 'Name', 'required');
		if ($this->form_validation->run() == TRUE){

            if($this->input->post('parent')==0 && $this->input->post('child')==0){
                $data['level']=0;
                $data['parent']=0;
            }else if($this->input->post('parent')!=0 && $this->input->post('child')==0){
                $data['level']=$this->input->post('parent');
                $data['parent']=$this->input->post('parent');
            }else {
                $data['level']=$this->input->post('parent');
                $data['parent']=$this->input->post('child');
            }
            $data['orderNo'] 		= $this->input->post('orderNo');

			$categoryId = $this->category_model->update($data,$id);
			if($categoryId) {
				redirect('cp/category');
			}
		}
		if(!empty($id)){
			$data['items']= $this->category_model->getSingle($id);
			$data['sub_level'] = $this->category_model->getListLevel($data['items']['level']);
		}

		$data['level'] = $this->category_model->getListLevel();
		$this->layout->view($data);
	}
    public function get_subcats($id){
        $data= get_category($id);
        $option = "<option value='0'>Please select...</option>";
        foreach($data as $val){
            $option.= "<option value='".$val['id']."'>".$val['name']."</option>";
        }
        echo json_encode(array('option'=>$option));
        exit();
    }


    public function delete($id){
        $this->category_model->delete($id);
      echo  json_encode(array('data'=>'ok'));
        exit();
    }


    public function deletelist() {
        $ids = $this->input->post ( 'id' );
        $this->category_model->delete($ids);
        $this->session->set_flashdata('result', 'Đã xóa thành công');
        redirect ( base_url ( '/cp/category') );
    }
}